<!--
  * 商户黑名单表
  *
  * @Author:    zzb
  * @Date:      2025-01-23 04:08:27
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="公司简称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.companyAbbrNameLeftLike" placeholder="公司简称" />
      </a-form-item>
      <a-form-item label="联系人" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.nameLeftLike" placeholder="联系人" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <div class="smart-table-operate-block" style="margin-left: auto">
        <a-button @click="showForm" type="primary" size="small" v-privilege="'merchant_blacklist:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'reason'">
          <a @click="showReasonDetail(record)">{{ record.reason }}</a>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="onDelete(record)" danger type="link" v-privilege="'merchant_blacklist:delete'">移除</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.page_size"
        v-model:current="queryForm.page_num"
        v-model:page_size="queryForm.page_size"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <MerchantBlacklistsForm ref="formRef" @reloadList="queryData" />

    <!-- 拉黑原因详情弹窗 -->
    <a-modal title="拉黑原因" :width="500" :open="reasonDetailVisible" @cancel="() => (reasonDetailVisible = false)" :footer="null">
      <div class="reason-detail">
        <div class="detail-item">
          <span class="label">拉黑处理：</span>
          <span>{{ currentDetail?.handleLabel }}</span>
        </div>
        <div class="detail-item">
          <span class="label">拉黑原因：</span>
          <span>{{ currentDetail?.reason }}</span>
        </div>
        <div class="detail-item">
          <span class="label">附件：</span>
          <div class="file-list">
            <div v-for="(file, index) in currentDetail?.files" :key="index" class="file-item">
              <a-image :src="file" alt="附件" />
            </div>
          </div>
        </div>
      </div>
    </a-modal>
  </a-card>
</template>
<script setup name="MerchantBlacklistList">
  import { reactive, ref, onMounted } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { merchantBlacklistApi } from '/src/api/business/b-operation/merchant-blacklist/merchant-blacklist-api'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import MerchantBlacklistsForm from './merchant-blacklist-form.vue'
  import { selectDictLabel } from '/@/utils/common.js'
  import {
    BLACKLIST_MERCHANT_STATUS, //拉黑状态
  } from '/@/constants/dict'
  //import FilePreview from '/@/components/support/file-preview/roles-user-list.vue'; // 图片预览组件

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      key: 'index',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.page_num - 1) * queryForm.page_size + index + 1
      },
      width: 60,
    },
    {
      title: '登录账号',
      dataIndex: 'no',
      ellipsis: true,
    },
    {
      title: '公司简称',
      dataIndex: 'companyAbbrName',
      ellipsis: true,
    },
    {
      title: '联系人',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '联系方式',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '地址',
      dataIndex: 'addr',
      ellipsis: true,
    },
    {
      title: '拉黑原因',
      dataIndex: 'reason',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------
  function showReasonDetail(record) {
    currentDetail.value = {
      ...record,
      handleLabel: selectDictLabel(BLACKLIST_MERCHANT_STATUS, record.handle),
      files: JSON.parse(record.url)?.map((item) => item.url),
    }
    console.log('currentDetail.value==>', currentDetail.value)

    reasonDetailVisible.value = true
  }

  const queryFormState = {
    keyword: undefined, //输入查询
    page_num: 1,
    page_size: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let page_size = queryForm.page_size
    Object.assign(queryForm, queryFormState)
    queryForm.page_size = page_size
    queryForm.companyAbbrNameLeftLike = ''
    queryForm.nameLeftLike = ''
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.page_num = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await merchantBlacklistApi.queryPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()

  function showForm() {
    formRef.value.show()
  }

  //确认删除
  function onDelete(data) {
    Modal.confirm({
      title: '提示',
      content: '确定要将该商户移除商户黑名单吗?',
      okText: '移除',
      okType: 'danger',
      onOk() {
        requestDelete(data)
      },
      cancelText: '取消',
      onCancel() {},
    })
  }

  //请求删除
  async function requestDelete(data) {
    SmartLoading.show()
    try {
      const params = {
        id: data.id,
      }
      await merchantBlacklistApi.delete(params)
      message.success('移除成功')
      queryData()
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      SmartLoading.hide()
    }
  }
  // ---------------------------- 批量删除 ----------------------------

  // 选择表格行
  const selectedRowKeyList = ref([])

  function onSelectChange(selectedRowKeys) {
    selectedRowKeyList.value = selectedRowKeys
  }

  // 批量删除
  function confirmBatchDelete() {
    Modal.confirm({
      title: '提示',
      content: '确定要批量删除这些数据吗?',
      okText: '删除',
      okType: 'danger',
      onOk() {
        requestBatchDelete()
      },
      cancelText: '取消',
      onCancel() {},
    })
  }

  //请求批量删除
  async function requestBatchDelete() {
    try {
      SmartLoading.show()
      await merchantBlacklistApi.batchDelete(selectedRowKeyList.value)
      message.success('删除成功')
      queryData()
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      SmartLoading.hide()
    }
  }

  // 拉黑原因详情相关
  const reasonDetailVisible = ref(false)
  const currentDetail = ref(null)
</script>

<style lang="less" scoped>
  .smart-table-btn-block {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
  }

  .smart-table-title {
    font-size: 16px;
    font-weight: 500;
  }

  .smart-table-operate-block {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .reason-detail {
    .detail-item {
      margin-bottom: 16px;

      .label {
        display: inline-block;
        width: 80px;
        color: #666;
      }
    }

    .file-list {
      display: flex;
      gap: 16px;
      margin-top: 8px;

      .file-item {
        width: 104px;
        height: 104px;
        border: 1px solid #d9d9d9;
        border-radius: 2px;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
  }
</style>
